package com.dys.testequals2;

import java.util.Objects;

/**
 * @className: com.dys.testequals2.Phone
 * @description: com.dys.testequals2
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/07/20 下午7:34
 */
public class Phone {
    // 属性封装
    private String brand;
    private double price;
    private int year;
    // 方法

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    // 构造器
    public Phone() {
    }

    public Phone(String brand, double price, int year) {
        this.brand = brand;
        this.price = price;
        this.year = year;
    }

    @Override
    public String toString() {
        return "Phone = {brand=" + brand + ", price=" + price + ", year=" + year + "}";
    }

    // @Override
    // public boolean equals(Object obj) {
    //     // 判断a对象是否为b类的实例,是返回true,不是返回false
    //     if (obj instanceof Phone) {
    //         // 转型
    //         Phone other = (Phone) obj;
    //         if (this.brand == other.getBrand() && this.price == other.getPrice() && this.year == other.getYear()) {
    //             return true;
    //         }
    //     }
    //     return false;
    // }
    public boolean equals(Object obj) {
        // 传同一个对象
        if (this == obj) {
            return true;
        }
        // 传入null
        if (obj == null || this.getClass() != obj.getClass()) {
            return false;
        }
        Phone phone = (Phone) obj;
        return Double.compare(phone.getPrice(), this.getPrice()) == 0 && year == phone.getYear() && Objects.equals(this.brand, phone.brand);
    }
}
